<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="等级名称">
        <el-input v-model="dataForm.levelName" clearable placeholder="等级名称" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="dataForm.status" clearable placeholder="请选择状态">
          <el-option :value="1" label="启用" />
          <el-option :value="0" label="禁用" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()" v-has-permission="['member:memberLevel:list']">查询</el-button>
        <el-button
          v-has-permission="['member:memberLevel:add']"
          type="primary"
          @click="addOrUpdateHandle()"
        >新增
        </el-button>
        <el-button
          v-has-permission="['member:memberLevel:delete']"
          :disabled="dataListSelections.length <= 0"
          type="danger"
          @click="deleteHandle()"
        >批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column align="center" header-align="center" type="selection" width="50" />
      <el-table-column align="center" header-align="center" label="id" prop="id" />
      <el-table-column align="center" header-align="center" label="等级图标" prop="icon">
        <template slot-scope="scope">
          <el-image :src="scope.row.icon" class="pic-image" :preview-src-list="[scope.row.icon]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="等级名称" prop="name" />
      <el-table-column align="center" header-align="center" label="总代充值折扣(单位：%)" prop="systemDiscount" />
      <el-table-column align="center" header-align="center" label="充值折扣(单位：%)" prop="discount" />
      <el-table-column align="center" header-align="center" label="下级充值折扣(单位：%)" prop="lowerDiscount" />
      <!--      <el-table-column align="center" header-align="center" label="下级推荐提成(单位：%)" prop="lowerMoney" />-->
      <!--      <el-table-column align="center" header-align="center" label="同级提货提成(单位：%)" prop="levelMoney" />-->
      <!--      <el-table-column align="center" header-align="center" label="下级提货提成(单位：%)" prop="lowerGoodsMoney" />-->
      <!--      <el-table-column align="center" header-align="center" label="最少提货金额(单位：元)" prop="leastMoney" />-->
      <el-table-column align="center" header-align="center" label="升级金额（一次性充值，单位：元）" prop="upgradeMoney" />
      <el-table-column align="center" header-align="center" label="首充金额(单位：元)" prop="firstRechargeMoney" />
      <el-table-column align="center" header-align="center" label="最少充值金额(单位：元)" prop="leastRechargeMoney" />
      <el-table-column align="center" header-align="center" label="最少提货金额(单位：元)" prop="leastMoney" />
      <el-table-column v-if="checkPermission(['member:memberLevel:edit'])" label="状态" align="center" width="160">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            loading
            active-text="启用"
            inactive-text="关闭"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          /></template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['member:memberLevel:edit', 'member:memberLevel:delete'])" align="center" fixed="right" header-align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="addOrUpdateHandle(scope.row.id)" v-has-permission="['member:memberLevel:edit']">修改</el-button>
          <el-button size="small" type="text" @click="deleteHandle(scope.row.id)" v-has-permission="['member:memberLevel:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px"
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />
  </div>
</template>

<script>
import AddOrUpdate from './member-level-add-or-update'
import { delMember_level, listMember_level, updateMember_level } from '@/api/ums/member_level'
import checkPermission from '@/utils/permission'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      dataForm: {
        name: undefined,
        status: undefined
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  async created() {
    this.getDataList()
  },
  methods: {
    checkPermission,
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const params = { pageNum: this.pageIndex, pageSize: this.pageSize }
      const levelName = this.dataForm.levelName
      const status = this.dataForm.status
      if (levelName) {
        params['levelName'] = levelName
      }
      if (status !== undefined) {
        params['status'] = status
      }
      listMember_level(params).then(({ data }) => {
        this.dataList = data.list
        this.totalPage = data.totalCount
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    handleStatusChange(val) {
      updateMember_level(val).then(res => {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.getDataList()
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      const ids = id
        ? [id]
        : this.dataListSelections.map(item => {
          return item.id
        })
      this.$confirm(
        `确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delMember_level(ids).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      })
    }
  }
}
</script>

<style scoped>
.pic-image {
  width: 40px;
  height: 40px;
  border-radius: 100%;
}
</style>
